//
//  EmoticonPackage.swift
//  01-表情键盘(绑定数据)
//
//  Created by gxiangzi on 15/9/14.
//  Copyright © 2015年 hqu. All rights reserved.
//

import UIKit

class EmoticonPackage: NSObject {
    
    // id
    var id : String?
    // 名称
    var group_name_cn:String?
    // 数组存储 emoticon 
    var emoticons = [Emoticon]()
    
    init(var dict:[String:AnyObject]) {
        super.init()
        
        id = dict["id"] as? String
        group_name_cn = dict["group_name_cn"] as? String
        
        // 获得一个
        if let emoArray = dict["emoticons"] as? [[String : String]]
        {

            var index = 0
            // 字典转模型
            for var emoDict in emoArray
            {
                
                if let filePath = emoDict["png"]
                {
                    emoDict["png"] = id! + "/" + filePath
                }
                
                emoticons.append(Emoticon(dict: emoDict))
                
                index++
                
                if index == 20
                {
                    emoticons.append(Emoticon(isRemove: true))
                    index = 0
                }
            }
            
        }
        
        // 追加空格
        appendBlack()
        
    }
    
    /// 追加空格
    private func appendBlack() {
        
        let count = emoticons.count % 21
        
        // 判断是否整出 并且 判断是否是最近的一项
        if count == 0 && emoticons.count > 0
        {
            return
        }
        
        for _ in count ..< 20
        {
            emoticons.append(Emoticon(isBlack: true))
        }
        
        emoticons.append(Emoticon(isRemove: true))
    }
    
    override var description: String
    {
        let keys = ["id", "group_name_cn", "emoticons"]
        
        return dictionaryWithValuesForKeys(keys).description
    }

}
